{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f920e9ce",
   "metadata": {},
   "source": [
    "# Discover Facts\n",
    "The following example shows how to predict new concept that are likely to be true via the __ampligraph.discovery.discover_facts__ API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f7d979c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../..')\n",
    "import os\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
    "os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'\n",
    "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n",
    "import tensorflow as tf\n",
    "tf.get_logger().setLevel('ERROR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dafa4c11",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ampligraph\n",
    "# Benchmark datasets are under ampligraph.datasets module\n",
    "from ampligraph.datasets import load_fb15k_237\n",
    "# load fb15k-237 dataset\n",
    "dataset = load_fb15k_237()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "87ed1243",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x4c0d7b760>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from ampligraph.latent_features import ScoringBasedEmbeddingModel\n",
    "\n",
    "# Create, compile and fit the model\n",
    "model = ScoringBasedEmbeddingModel(eta=1, \n",
    "                                   k=100,\n",
    "                                   scoring_type='ComplEx')\n",
    "\n",
    "\n",
    "\n",
    "model.compile(optimizer='adam', \n",
    "              loss='multiclass_nll')\n",
    "\n",
    "model.fit(dataset['train'],\n",
    "          batch_size=10000,\n",
    "          epochs=50,\n",
    "          verbose=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a3cc5988",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([['/m/0342h', '/location/country/form_of_government', '/m/09nqf'],\n",
       "        ['/m/0cnk2q', '/location/country/form_of_government', '/m/01q03']],\n",
       "       dtype=object),\n",
       " array([ 5.5, 54.5]))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from ampligraph.discovery import discover_facts\n",
    "\n",
    "discover_facts(dataset['train'][:100], \n",
    "               model, \n",
    "               top_n=100, \n",
    "               strategy='entity_frequency', \n",
    "               max_candidates=100,\n",
    "               target_rel='/location/country/form_of_government', \n",
    "               seed=0)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "vscode": {
   "interpreter": {
    "hash": "2e69f3670cdad0193847aaa0b77be56c05c951fcbdd384ff882dde0464f4de76"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
